﻿using System;
using System.ComponentModel;
using System.Security.Cryptography;

namespace BitTorrent.Client.Peer
{
    public class PeerId
    {
        private readonly byte[] _peerId;

        public PeerId() {
            /// REVISIT: Temp hack.
            using (SHA1 sha = new SHA1CryptoServiceProvider()) {
                _peerId = sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(DateTime.MaxValue.ToString()));
            }
        }

        public PeerId(byte[] peerId) {
            _peerId = peerId;
        }

        public int Length {
            get {
                return _peerId.Length;
            }
        }

        public byte[] Bytes {
            get {
                return _peerId;
            }
        }

        public enum Client
        {
            [Description("µTorrent")]
            UTorrent
        }
    }

    public static class PeerIdClientExtention
    {
        public static string ToName(this PeerId.Client value) {
            var descriptions = ((DescriptionAttribute[])value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false));

            return descriptions.Length > 0 ? descriptions[0].Description : value.ToString();
        }
    }
}
